<?php
/**
 * Description of Cliente
 *
 * @author Nelifer
 */
include_once('BDManager.class.php');

class Cliente {
    var $con;
    function Cliente(){
        $this->con=new BDManager();
    }

    function insertar($campos){
        if($this->con->conectar()==true){
            $sql = "INSERT INTO clientes (cli_rif, cli_razonsocial, cli_estatus, cli_tipopersona, cli_direccion, cli_email, cli_tlfoficina, ".
                   "cli_tlfmovil, cli_tlfadicional, cli_fechacreacion, cli_horacreacion, usu_id ) ".
                   "VALUES ('".$campos[0]."','".$campos[1]."','".$campos[2]."','".$campos[3]."','".$campos[4]."','".$campos[5]."','".$campos[6]."','".
                               $campos[7]."','".$campos[8]."','".$campos[9]."','".$campos[10]."','".$campos[11]."')";
            return mysql_query($sql);
        }
    }

    function actualizar($campos, $id){
        if($this->con->conectar()==true){
            $sql = "UPDATE clientes SET cli_rif = '".$campos[0]."', cli_razonsocial = '".$campos[1]."', cli_tipopersona = '".$campos[2]."', ".
                   "cli_direccion = '".$campos[3]."', cli_email = '".$campos[4]."', cli_tlfoficina = '".$campos[5]."', cli_tlfmovil = '".$campos[6]."', " .
                   "cli_tlfadicional = '".$campos[7]."' WHERE cli_id = ".$id;
            return mysql_query($sql);
        }
    }

    function getConsulta($id){
        if($this->con->conectar()==true){
            return mysql_query("SELECT * FROM clientes WHERE cli_id=".$id);
        }
    }

    function getConsultas(){
        if($this->con->conectar()==true){
            return mysql_query("SELECT cli_id, pre_nombre, pre_unidad FROM clientes ORDER BY cli_id DESC");
        }
    }

    function eliminar($id){
        if($this->con->conectar()==true){
            return mysql_query("DELETE FROM clientes WHERE cli_id=".$id);
        }
    }
    private function countRec($fname,$tname,$where) {
        $con = null;
        $sql = "SELECT count($fname) FROM $tname $where";
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $result = mysql_query($sql);
        }
        while ($row = mysql_fetch_array($result)) {
            return $row[0];
        }
    }
    
    public function consultar_grid($page=null, $rp=null, $sortname=null, $sortorder=null, $query=null, $letter_pressed=null, $qtype=null){
        if (!$sortname) $sortname = 'name';
        if (!$sortorder) $sortorder = 'desc';
        if($query!=''){
             $where = "WHERE `".$qtype."` LIKE '%".$query."%' ";
        } else {
            $where ='';
        }
        if($letter_pressed!=''){
            $where = "WHERE `".$qtype."` LIKE '".$letter_pressed."%' ";	
        }
        if($letter_pressed=='#'){
            $where = "WHERE `".$qtype."` REGEXP '[[:digit:]]' ";
        }
        $sort = "ORDER BY $sortname $sortorder";

        if (!$page) $page = 1;
        if (!$rp) $rp = 10;
        $start = (($page-1) * $rp);
        $limit = "LIMIT $start, $rp";
        $sql = "SELECT cli.cli_id, cli.cli_rif, cli.cli_razonsocial, vva.vva_desc cli_tipopersonaD, ". 
                      "cli.cli_direccion, CONCAT(usu.usu_nombre, ' '  ,usu.usu_apellido) usu_nombre ".
               "FROM clientes cli ".
                    "INNER JOIN usuarios usu on usu.usu_id = cli.usu_id ".
                    "INNER JOIN valoresvariables vva on vva.vva_valor = cli.cli_tipopersona and vva.vva_cod = 'tpo_per' $where $sort $limit";
        //echo "SQL: ". $sql;
        $con = $this->con->conectar();
        $result = mysql_query($sql);
        //$this->con->close($con);
        $total = $this->countRec('cli_id','clientes',$where);
        // Return JSON data
        $data = array();
        $data['page'] = $page;
        $data['total'] = $total;
        $data['rows'] = array();
        while ($row = mysql_fetch_assoc($result)) {            
            $data['rows'][] = array(
            'id' => $row['cli_id'],
            'cell' => array($row['cli_id'], $row['cli_rif'], $row['cli_razonsocial'], $row['cli_tipopersonaD'], $row['cli_direccion'], $row['usu_nombre'] )
            );
        }
        echo json_encode($data);
    }
    public function getItemsCliente(){
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $param = trim($_GET['term']);

            $sql = "SELECT cli_id, cli_rif, cli_razonsocial FROM clientes where (cli_razonsocial like '%".$param ."%' OR '".$param."' = '' ) OR (cli_rif like '%".$param ."%' OR '".$param."' = '' )";
            $result = mysql_query($sql);

            $data   = array();
            while ($row = mysql_fetch_assoc($result)) {
                $row_array['id'] = $row['cli_id'];
                $row_array['value'] = $row['cli_rif'] . " / " .$row['cli_razonsocial'];
                $row_array['abbrev'] = $row['cli_razonsocial'];
                array_push($data,$row_array);

            }
        }
        echo json_encode($data);
        
    }  
    public function getArrayCliente( $cliId = null ){
        $result = $this->getConsulta($cliId);

        $row_array   = array();
        while ($row = mysql_fetch_assoc($result)) {
            $row_array['id']     = $row['CLI_ID'];
            $row_array['cirif']  = $row['CLI_RIF'];
            $row_array['razonS'] = $row['CLI_RAZONSOCIAL'];
            $row_array['direcF'] = $row['CLI_DIRECCION'];
            $row_array['tlfOfi'] = $row['CLI_TLFOFICINA'];
            //array_push($data,$row_array);

        }
        //print_r($row_array);
        return $row_array;
        
    }    
}

?>
